package cn.anlaser.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import cn.anlaser.admin.BaseUser;
import cn.anlaser.entity.CommonEntity;
import cn.anlaser.entity.RelatedEntity;
import cn.anlaser.form.RelatedForm;
import cn.anlaser.repository.RelatedRepository;
import cn.anlaser.service.RelatedService;
import cn.anlaser.status.FlagEnum;
import cn.anlaser.utils.BeanMapper;
import cn.wsalix.utils.UserUtils;

public abstract class RelatedServiceImpl<Form extends RelatedForm, Repository extends RelatedRepository<Form, Entity,Prefix, Postfix>, Entity extends RelatedEntity<Prefix,Postfix>, Prefix extends CommonEntity, Postfix extends CommonEntity>
		extends CommonServiceImpl<Form, Repository, Entity> implements
		RelatedService<Form, Repository, Entity, Prefix, Postfix> {
	@Autowired
	protected UserUtils userUtils;
	@Transactional
	@Override
	public Entity setRelated(Form form) {
		BaseUser user = userUtils.getUser();
		Entity entity = BeanMapper.map(form, getEntityClazz());
		entity.setCreatedDate(new Date());//DateTime.now());
		if (user != null) {
			entity.setCreatedBy(user.getId());
		}
		entity.setFlag(FlagEnum.valid);
		entity.setPreFix(findPreFix(form.getPreFixId()));
		entity.setPostFix(findPostFix(form.getPostFixId()));
		getRepository().save(entity);
		return entity;
	}
	
}
